
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

struct TreeNode* func(int* preorder,int* inorder,int l1,int l2,int r1,int r2){
    if(l1>r1) return NULL;
    //创建一个新节点用来保存头节点
    struct TreeNode* p=(struct TreeNode*)malloc(sizeof(struct TreeNode));
    p->val=preorder[l1];
    p->left=NULL;
    p->right=NULL;
    int mid;
    for(int i=l2;i<=r2;i++){
        if(inorder[i]==preorder[l1]){
            mid=i;
            break;
        }
    }
    int i=mid;
    p->left=func(preorder,inorder,l1+1,l2,l1+i-l2,i-1);
    p->right=func(preorder,inorder,l1+i-l2+1,i+1,r1,r2);
    return p;
}
struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int inorderSize){
    return func(preorder,inorder,0,0,preorderSize-1,inorderSize-1);
}
